﻿网页上的增删改查
using Common;
using Core;
using DAL.DAO;
using DAL.Entity;
using DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UI.Infrastructure;

namespace UI.Controllers
{
    public class ${ClassName}Controller : Controller
    {


        private ${TableName}DAO ${TableName}Dao { get; set; }

        private user_device_authorizationDAO user_device_authorizationDao { get; set; }

        private device_user_bindDAO device_user_bindDao { get; set; }


        public ${ClassName}Controller() {

            if (${TableName}Dao ==null)
            {
                ${TableName}Dao = new ${TableName}DAO();
            }

            if (user_device_authorizationDao == null)
            {
                user_device_authorizationDao = new user_device_authorizationDAO();
            }

            if (device_user_bindDao == null)
            {
                device_user_bindDao = new device_user_bindDAO();
            }



        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ${ClassName}List()
        {
            ViewBag.type = Codebooks.CODE_${TableNameUpper};
            return PartialView();
        }



        /// <summary>
        ///  设备列表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="ol_name"></param>
        /// <param name="sw_imei"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ${ClassName}Filter(string sn, string type, string user_name, string page, string rows)
        {
            var args = ValidateHelper.IsNotNullOrEmpty(page, rows);
            if (!args.Flag)
                return Json(args, JsonRequestBehavior.AllowGet);
            int total = 0, records = 0;

            var data = ${TableName}Dao.GetDeviceFilter(sn, user_name, Convert.ToInt32(page), Convert.ToInt32(rows), ref total, ref records);
            return Json(new
            {
                rows = data,
                total = total,
                records = records
            }, JsonRequestBehavior.AllowGet);
        }




        /// <summary>
        /// 编辑 返回页面
        /// </summary>
        /// <param name="sw_id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Edit${ClassName}(string id)
        {
            var args = ValidateHelper.IsNotNullOrEmpty(id);
            if (!args.Flag)
                return PartialView();
            var result = ${TableName}Dao.GetById(id.ToInt64());

            return PartialView(result);
        }


        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit${ClassName}Do([ModelBinder(typeof(UserInfoBinder))] UserEntity @operator, ${TableName} model)
        {
            var args = new ArgsHelper();
            if (model.id == 0)
            {
                args.Flag = false;
                args.Msg = "非法参数";
                return Json(args);
            }
            model.updater = @operator.Us_ID;
            model.update_date = DateTime.Now;
            args = ${TableName}Dao.Update(model);
            return Json(args);
        }



        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="w_id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete${ClassName}(string id)
        {
            var args = ValidateHelper.IsNotNullOrEmpty(id);
            if (!args.Flag)
                return Json(args);

            var relation = device_user_bindDao.GetRelationByDivice(Convert.ToInt64(id), Codebooks.CODE_${TableNameUpper});
            if (relation != null)
            {
                return Json(new
                {
                    Flag = false,
                    Msg = "请先解绑用户，再删除设备",
                });
            }
            else
            {
                args = ${TableName}Dao.DeleteById(Convert.ToInt64(id));
                return Json(args);
            }
        }
        #endregion


        #region 绑定
        [HttpPost]
        public ActionResult Bind${ClassName}([ModelBinder(typeof(UserInfoBinder))] UserEntity model, string type, string user_id, string device_id)
        {
            var args = ValidateHelper.IsNotNullOrEmpty(user_id, device_id, type);
            if (!args.Flag)
                return Json(args);
            var Grant = user_device_authorizationDao.GetAuth(user_id.ToInt64(), type.ToInt32());
            if (Grant == null)
                return Json(new ArgsHelper("没有权限进行该操作"));
            var result = device_user_bindDao.Add(new device_user_bind
            {
                create_date = DateTime.Now,
                creator = model.Us_ID,
                device_type = Grant.device_type,
                device_id = device_id.ToInt64(),
                user_id = user_id.ToInt64()
            });
           
            return Json(result);
        }

        #endregion


        #region 解绑
        [HttpPost]
        public ActionResult DeleteBind${ClassName}(string relation_id)
        {
            var args = ValidateHelper.IsNotNullOrEmpty(relation_id);
            if (!args.Flag)
                return Json(args);
            var result = device_user_bindDao.Delete(relation_id.ToInt64());
         
            return Json(result);
        }

        #endregion
    }
}